package hotel.menu.activity;

import hotel.menu.untils.FlowIndicator;

import com.example.youknow.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class Guide extends Activity implements OnClickListener,
		OnGestureListener, OnTouchListener {
	private FlowIndicator mMyView;
	private ViewFlipper viewFlipper;
	private int fling_min_distance = 50;
	private int fling_min_velocity = 0;
	private int mCurrPos = 0;
	private int[] mImages = new int[] { R.drawable.welcome1,
			R.drawable.welcome2, R.drawable.welcome3, R.drawable.welcome4 };
	private LayoutInflater mInflater;
	private GestureDetector mGestureDetector;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.guide);
		findID();
		setviewpage();
	}

	private void setviewpage() {
		mGestureDetector = new GestureDetector(this);
		setView(mCurrPos, 0);
		mMyView.setCount(mImages.length);
		mMyView.setSeletion(0);
	}

	private void findID() {
		viewFlipper = (ViewFlipper) this.findViewById(R.id.guide_viewflipper);
		mMyView = (FlowIndicator) this.findViewById(R.id.guide_flowIndicator);
		mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		viewFlipper.setOnTouchListener(this);
		viewFlipper.setLongClickable(true);
		viewFlipper.setOnClickListener(this);
	}

	@Override
	public boolean onDown(MotionEvent arg0) {
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if (e1.getX() - e2.getX() > fling_min_distance
				&& Math.abs(velocityX) > fling_min_velocity) {
			showNextView();
		} else if (e2.getX() - e1.getX() > fling_min_distance
				&& Math.abs(velocityX) > fling_min_velocity) {
			showPreviousView();
		}
		return false;
	}

	private void showNextView() {
		if (mCurrPos + 1 == -2) {

		} else if (mCurrPos + 1 == mImages.length) {
			finish();
		} else {
			setView(mCurrPos, mCurrPos + 1);
			viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_in));
			viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_left_out));
			viewFlipper.showNext();
			mMyView.setSeletion(mCurrPos);
		}

	}

	private void showPreviousView() {

		if (mCurrPos - 1 == -1) {

		} else if (mCurrPos - 1 == mImages.length) {
			finish();
		} else {
			setView(mCurrPos, mCurrPos - 1);
			viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_in));
			viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
					R.anim.push_right_out));
			viewFlipper.showPrevious();

			mMyView.setSeletion(mCurrPos);
		}

	}

	private void setView(int curr, int next) {

		View v = (View) mInflater.inflate(R.layout.flipper_item, null);
		ImageView iv = (ImageView) v.findViewById(R.id.img);
		iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
		if (curr < next && next > mImages.length - 1)
			next = 0;
		else if (curr > next && next < 0)
			next = mImages.length - 1;
		iv.setImageResource(mImages[next]);
		if (viewFlipper.getChildCount() > 1) {
			viewFlipper.removeViewAt(0);
		}
		viewFlipper.addView(v, viewFlipper.getChildCount(), new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		mCurrPos = next;
	}

	@Override
	public void onLongPress(MotionEvent e) {

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return mGestureDetector.onTouchEvent(event);
	}

	@Override
	public void onClick(View arg0) {

	}

}
